﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTime>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    cmdStorage = new CDataStorage();

    ui->setupUi(this);
    QWidget *widget = new QWidget();
    this->setCentralWidget(widget);

    QVBoxLayout *vblayout = new QVBoxLayout();

    button = new QPushButton("开始");
    button->setFixedWidth(60);
    buttonOpen = new QPushButton("打开");
    buttonOpen->setFixedWidth(60);
    checkBox = new QCheckBox("暂停");
    QHBoxLayout *hblayout1 = new QHBoxLayout();
    hblayout1->addWidget(button);
    hblayout1->setSpacing(20);
    hblayout1->addWidget(buttonOpen);
    hblayout1->setSpacing(20);
    hblayout1->addWidget(checkBox);

    dataWidget = new CDataWidget();
//    scrollBar = new QScrollBar();
//    QHBoxLayout *hblayout2 = new QHBoxLayout();
//    hblayout2->addWidget(dataWidget);
//    hblayout2->addWidget(scrollBar);

    vblayout->addLayout(hblayout1);
//    vblayout->addLayout(hblayout2);
    vblayout->addWidget(dataWidget);
    vblayout->setStretchFactor(hblayout1,1);
    vblayout->setStretchFactor(dataWidget,15);
    centralWidget()->setLayout(vblayout);

    connect(button,SIGNAL(clicked()),this,SLOT(OnClickBt1()));
    connect(buttonOpen,SIGNAL(clicked()),this,SLOT(OnClickBt2()));
    connect(dataWidget,SIGNAL(updateview()),dataWidget,SLOT(UpdateView()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::OnClickBt1()
{
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(OnTime1out()));
    timer->start(1);
    cmdStorage->SetSaveFile("E:/Working/Qt/Lerning/QTRoad/58-DrawText-LineEdit-Plus1/save.txt");
}

void MainWindow::OnTime1out()
{
    if(checkBox->isChecked()) return;
    static qint32 count=0;
    count++;
    QByteArray arr1,arr2;
    char ctag[2]={0xAA,0xCC};
    char att = qrand()%2;
    arr1[0]=ctag[att];
    arr1[1]=ctag[1-att];
    for(int i=2;i<23;i++)
    {
        arr1[i] = i-1;
    }
//    memcpy(arr.data(),&ount,sizeof(count));

    //时间数据
    QTime current_time = QTime::currentTime();
//    int hour = current_time.hour();
//    int minute = current_time.minute();
//    int second = current_time.second();
    int msec = current_time.msec()%100;
    //计数器数据
//    int msec = count%100;
    arr1[23] = msec/10*16+msec%10;
//    for(int i=0;i<arr1.count();i++)
//    {
//        arr2.push_back(arr1[i]);
//        arr2.push_back(0x7E);
//    }
//    dataWidget->datalist.push_back(arr1);
//    dataWidget->AddToDataList(arr1);
    cmdStorage->AddToListData(arr1);
    if(count%43==0)
    {
        emit dataWidget->updateview();
    }
}

void MainWindow::OnClickBt2()
{
    QFile file("E:/Working/Qt/Lerning/QTRoad/58-DrawText-LineEdit-Plus1/save.txt");
    if(file.open(QIODevice::ReadOnly))
    {
        QDataStream in(&file);
        QByteArray arr;
        while(!in.atEnd())
        {
            in>>arr;
            cmdStorage->AddToListData(arr);
            emit dataWidget->updateview();
        }
        file.close();
    }
}
